package com.geek.auth;

import com.geek.auth.service.ApiAuthenticator;
import com.geek.auth.service.DefaultApiAuthenticatorImpl;
import com.geek.auth.util.SignUtils;

public class Client {
    public static void main(String[] args) {
        // 加密前的参数, 以下参数排列要按照ascii顺序从小到大排列
        // appId: 对应内部系统的appSecret  即密钥，加密时需要带上
        // timestamp: 根据时间戳来防止过期请求
        // bizContent： 接口具体所用参数，例如账户号，{"accountNo":"ali123"}，以下为特殊字符经url编码后的文本
        String plain = "appId=123456&bizContent=%7B%22accountNo%22%3A%22ali123%22%20%7D&timestamp=20210119110058";
        String token = sign(plain);

        // 真正的请求，参数不需要排序
        String request = "appId=123456&token=" + token + "&timestamp=20210119110058&bizContent=%7B%22accountNo%22%3A%22ali123%22%20%7D";
        ApiAuthenticator authencator = new DefaultApiAuthenticatorImpl();
        authencator.auth(request);
    }

    // 模拟客户端加密
    public static String sign(String plain) {
        return SignUtils.sign(plain, "123", "utf-8");
    }
}
